/*
 * Dbuf.cpp
 *
 *  Created on: Mar 2, 2011
 *      Author: ramble
 */

#include "Dbuf.h"

CDBuf::CDBuf()
{
	buf1state = 0;
	buf2state = 0;
	freshtag = -1;
}
CDBuf::~CDBuf()
{

}

bool CDBuf::PushData(unsigned char * data,long size)
{
	if( data == NULL || size <= 0 || size >= MAXBUFSIZE)
	{
		return false;
	}
	if(buf1state == 0)
	{
		buf1state = size;
		memcpy(buf1,data,size);
		freshtag = 1;
		return true;
	}

	if(buf2state == 0)
	{
		buf2state = size;
		memcpy(buf2,data,size);
		freshtag = 2;
		return true;
	}
	return false;
}

bool CDBuf::GetData(unsigned char * data,long & size)
{
	if(freshtag == -1)
	{
		size = 0;
		return false;
	}
	if(freshtag == 1)
	{
		if(buf2state != 0)
		{
			memcpy(data,buf2,buf2state);
			size = buf2state;
			buf2state = 0;
			return true;
		}
		if(buf1state != 0)
		{
			memcpy(data,buf1,buf1state);
			size = buf1state;
			buf1state = 0;
			return true;
		}
		return false;
	}

	if(freshtag == 2)
	{

		if(buf1state != 0)
		{
			memcpy(data,buf1,buf1state);
			size = buf1state;
			buf1state = 0;
			return true;
		}
		if(buf2state != 0)
		{
			memcpy(data,buf2,buf2state);
			size = buf2state;
			buf2state = 0;
			return true;
		}
		return false;
	}
	return false;
}
